package com.swt.testdemo.shiro;


import com.swt.testdemo.entity.SysUserEntity;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.subject.Subject;

/**
 * shiro工具类
 *
 * @author BleethNie
 * @version 1.0
 * @date 2020-01-09 09:48
 */
public class ShiroUtils {
    /**
     * 加密算法
     */
    public static final String HASH_ALGORITHM_NAME = "SHA-256";

    /**
     * 循环次数
     */
    public static final int HASH_ITERATIONS = 16;


    public static String sha256(String password, String salt) {
        return new SimpleHash(HASH_ALGORITHM_NAME, password, salt, HASH_ITERATIONS).toString();
    }


    public static Subject getSubject() {
        return SecurityUtils.getSubject();
    }


    public static SysUserEntity getUser() {
        SysUserEntity userEntity = (SysUserEntity) getSubject().getPrincipal();
        if (userEntity == null) {
            return null;
        }
        return userEntity;
    }

    public static Integer getUserId() {
        SysUserEntity userEntity = (SysUserEntity) getSubject().getPrincipal();
        if (userEntity == null) {
            return null;
        }
        return userEntity.getUserId();
    }

    public static void logout() {
        SecurityUtils.getSubject().logout();
    }


}
